
package jgf.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import jgf.Arc;
import jgf.Attribute;
import jgf.Node;

/**
 * This bean is a piece of information about a generic Node.
 * It manages all the information relevant to a Node,
 * i.e. The ID, the label, the list of attributes and the graph which it belongs to.<br />
 * This class should be totally populated to avoid undesired effects.
 * The list of attributes represented here should correspond with
 * the default list of the graph for nodes (only the key set).
 * @author Marcos Garcia Ortega <a href="mailto:thiamath@gmail.com">&lt;thiamath@gmail.com&gt;</a>
 */
public class NodeBean  implements Serializable{

	protected Integer graphId;

	protected Integer id;
	protected String label;
	protected TreeMap<String, Integer> attributes;
	protected ArrayList<Integer> linkedIn;
	protected ArrayList<Integer> linkedOut;

	/** Creates an empty bean of information about a node. */
	public NodeBean() {
	}

	/**
	 * Creates a NodeBean with the data from the given Node
	 * @param node The template node to build the NodeBean.
	 */
	public NodeBean(Node node){
		id = node.getId();
		label = node.getLabel();
		this.setAttributes(node.getAttributeList());
		linkedIn = new ArrayList<Integer>();
		linkedOut = new ArrayList<Integer>();
		for (Arc arc : node.getLinkedArcs()) {
			if(arc.getTo().getId() == id)
				linkedIn.add(arc.getId());
			else
				linkedOut.add(arc.getId());
		}
	}

	/**
	 * Gets the ID number of the node.
	 * @return An Integer with the ID of the node.
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * It sets the ID for the node that represents this bean.
	 * @param id The ID of the represented node.
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Gets the label of the node.
	 * @return A String with the label of the node.
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * Sets the label of the represented node.
	 * @param label A String with the label of the node.
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * Gets the list of attributes assigned to the node.
	 * @return A List of Attribute with all the attributes of the node.
	 */
	public List<Attribute> getAttributes() {
		List<Attribute> ret = new ArrayList<Attribute>();

		for (String key : this.attributes.keySet()) {
			ret.add(new Attribute(key, this.attributes.get(key)));
		}

		return ret;
	}

	/**
	 * Populates the bean with all the attributes passed as parameter that belong to the node.
	 * @param attributes A List of Attribute with the attributes of the node.
	 */
	public void setAttributes(List<Attribute> attributes) {
		for (Attribute attribute : attributes) {
			this.attributes.put(attribute.getName(), attribute.getValue());
		}
	}

	/**
	 * Gets the ID number of the graph which this node belongs to.
	 * @return An Integer with the ID number of the graph.
	 */
	public Integer getGraphId() {
		return graphId;
	}

	/**
	 * Sets the ID number of the graph which this node belongs to.
	 * @param graphId An Integer with the ID number of the graph.
	 */
	public void setGraphId(Integer graphId) {
		this.graphId = graphId;
	}

	/**
	 * Gets the list of inbound arcs (their ID numbers) of the node.
	 * @return A List of Integer with the IDs of the arcs that comes into the node.
	 */
	public List<Integer> getLinkedIn() {
		return linkedIn;
	}

	/**
	 * Sets the list of inbound arcs (their ID numbers) of the node.
	 * @param linkedIn A List of Integer with the IDs of the arcs that comes into the node.
	 */
	public void setLinkedIn(List<Integer> linkedIn) {
		for (Integer arc : linkedIn) {
			this.linkedIn.add(arc);
		}
	}

	/**
	 * Gets the list of outbound arcs (their ID numbers) of the node.
	 * @return A List of Integer with the IDs of the arcs that leaves from the node.
	 */
	public List<Integer> getLinkedOut() {
		return linkedOut;
	}

	/**
	 * Sets the list of outbound arcs (their ID numbers) of the node.
	 * @param linkedIn A List of Integer with the IDs of the arcs that leaves from the node.
	 */
	public void setLinkedOut(List<Integer> linkedOut) {
		for (Integer arc : linkedOut) {
			this.linkedOut.add(arc);
		}
	}


}
